<div style="margin-top: 0px; height: auto; overflow-x: hidden;background:#F2F2F2;">
<div  class="shadowBox" style="margin-bottom:10px">
<div class="height40 TextMiddle40 card_item_toolbar">
<a href="#" onclick="javascript:myTaskJs.changeType('assignee','all')" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-task'">所有任务</a>
|<a href="#" onclick="javascript:myTaskJs.changeType('assignee','processing')" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-processing_task'">未完成任务</a>
|<a href="#" onclick="javascript:myTaskJs.changeType('assignee','approving')" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-wait_task'">审批中任务</a>
|<a href="#" onclick="javascript:myTaskJs.changeType('assignee','reject')" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-reject_task'">被退回任务</a>
|<a href="#" onclick="javascript:myTaskJs.changeType('assignee','complete')" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-complete_task'">已完成任务</a>
|<a href="#" onclick="javascript:myTaskJs.changeType('assignee','expired')" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-expired_task'">已过期任务</a>
|<a href="#" onclick="javascript:myTaskJs.changeType('assigner','all')" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">我分配的任务</a>
</div>
</div>
<div id="Task_records" class="cardItems">
</div>
</div>
<div class="tabLink">
	<ul>
		<li><a href="javascript:myTaskJs.prePage()">上一页</a></li>
		<li><a href="javascript:myTaskJs.nextPage()">下一页</a></li>
	</ul>
</div> 

<div id="completeTask" title="完成任务" class="easyui-dialog"  closed="true" style="width:400px;height:300px">
	<form>
		
		    <div class="ftitle"></div>
        	<input id="DiagTaskId" type="hidden">
            <div>
                <textarea id="completeTaskResponse" style="height:100px;width:90%;margin-left:10px"></textarea>
            </div>
            <div style="text-align:center;padding:5px">
            	<a href="javascript:void(0)" class="easyui-linkbutton" onclick="myTaskJs.submitTaskAction()">提交</a>
        	</div>
        	
	</form>
</div>
<div id="relateDiag"  class="easyui-dialog"  title="任务相关项" closed="true" style="width:900px;height:550px">
</div>
<script type="text/javascript">
(function(myTaskJs, $, undefined ) {
	
	var total = 0;
	var currentPage = 1;
	var limit = 5;
	var type = 'assignee';
	var state= 'all';

	myTaskJs.changeType = function(ty,st){
		type = ty;
		state = st;
		currentPage = 1;
		refresh();
	};
	
	function refresh(){
		var start = (currentPage - 1) * limit;
		var url = "dispatch/utility/task/mine?type="+type+"&start="+start+"&limit="+limit;
		if(state != 'all')
			url +="&state="+state;
		workbenchJs.loadData(url,myTaskJs.loadTaskResult,"查询任务");
	}
	function rowOver(id){
		$(id).addClass('row-over');
	}
	function rowOut(id){
		$(id).removeClass('row-over');
	}
	
	myTaskJs.nextPage = function()
	{
		if(currentPage * limit >= total)
		{
			$.messager.alert("Error",'已经是最后一页了');
		}else
		{
			currentPage = currentPage +1;
			refresh();
			
		}
	};
	myTaskJs.prePage = function()
	{
		if(currentPage == 1)
		{
			$.messager.alert("Error",'已经是第一页了');
		}else
		{
			
			currentPage = currentPage -1;
			refresh();
		}
	};
	function itemClick(id){

		var desc = $(id).parent().find('.card_item_desc');
		
		if($(desc).is(':visible')){
			$(desc).hide();
		}else
		{
			var task = $(id).parent().attr('task');
			var activity = $(id).parent().find('.card_item_desc').find('.task_item_activity');
			$.ajax({
	            type: "post",
	            url: "dispatch/utility/task/activity?id="+task,
	            dataType:"json",
	            success: function (result) {
	            	 if(result.result == "SUCCESS")
	                 {
	            		 var value = jQuery.parseJSON(result.payload.value);
	            		 var cc = [];
	            		 $(value).each(function(index){
	            			 cc.push('<span class="card_label">');
	            			 cc.push(value[index].time);
	            			 cc.push('<span class="card_label">');
	            			 cc.push(value[index].user);
	            			 cc.push('</span><span class="card_label">');
	            			 cc.push(value[index].content);
	            			 cc.push('</span><br>');
	            		 });
	            		 $(activity).html(cc);
	                 }
	            	
	            }
	            
	        });
			$(desc).show();
		}
		
	
	}
	var completeDiv;
	var url;
	myTaskJs.openApplyApproveTask = function(id,divId){
		completeDiv = divId;
		$('#DiagTaskId').val(id);
		$('#completeTask').dialog("open");
		$('#completeTaskResponse').val('');
		url = "dispatch/utility/task/applyApprove";
	}
	myTaskJs.openApproveTask = function(id,divId){
		completeDiv = divId;
		$('#DiagTaskId').val(id);
		$('#completeTask').dialog("open");
		$('#completeTaskResponse').val('');
		url = "dispatch/utility/task/approve";
	}
	myTaskJs.openRejectTask = function(id,divId){
		completeDiv = divId;
		$('#DiagTaskId').val(id);
		$('#completeTask').dialog("open");
		$('#completeTaskResponse').val('');
		url = "dispatch/utility/task/reject";
	}
	myTaskJs.submitTaskAction = function(){
		var str = "id="+$('#DiagTaskId').val()+"&response="+$('#completeTaskResponse').val();
		$.ajax({
            type: "post",
            url: url,
            dataType:"json",
            data: str,
            cache: false,
            success: function (result) {
            	var res = result.result;
                if(res == "SUCCESS")
                {
                	$('#completeTask').dialog("close");
                	$('#'+completeDiv).hide();
                }else if(res == "COMPONENT_ERROR")
                {
                	$.messager.alert("Error",result.payload.value);
                }else
                {
                	$.messager.alert("Error",result.result);
                }
                
            }
            
        });
	}
	function convertState(state){
		if(state == 'processing')
			return '进行中';
		else if(state == 'complete')
			return '完成';
		else if(state == 'expired')
			return '已过期';
		else if(state == 'approving')
			return '正在批准';
		else if(state == 'reject')
			return '被拒绝';
	}
	function convertLevel(level){
		if(level = 'normal')
			return '普通';
		if(level = 'high')
			return '高';
	}
	function getRelateLink(type,id){
		if(type == 'opportunity')
			return 'component/customer/viewOpportunity.jsp?id='+id;
	}
	myTaskJs.openRelate = function(url){
		$('#relateDiag').dialog("open");
		$('#relateDiag').dialog('refresh',url);
	}
	
	myTaskJs.loadTaskResult = function(result){
		
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	var value = jQuery.parseJSON(result.payload.value);
        	total = value.total;
        	value = value.rows;
        	var id = 0;
        	var cc = [];
        	$(value).each(function(index){
        		var record = value[index];
        		id++;
        		var idName = "task_item_"+id;
        		cc.push('<div id="');
        		cc.push(idName);
        		cc.push('" class="card_item" task="'+record.id+'"><div class="rappidLink_Pic"><img src="images/');
        		if(record.state == 'processing')
        			cc.push('processing.jpg');
        		else if(record.state == 'complete')
        			cc.push('complete.jpg');
        		else if(record.state == 'expired')
        			cc.push('expired.jpg');
        		else if(record.state == 'reject')
        			cc.push('reject.jpg');
        		else if(record.state == 'approving')
        			cc.push('wait.png');
        		
        		cc.push('" style="width:50px;"></div><div class="card_otherInfo">');
        		
        		cc.push('<p style="height:24px;line-height:24px;margin:0;padding:0">标题  : ');
        		cc.push(record.title)
        		cc.push('</p><span class="card_label">创建者 : ');
        		cc.push(record.assigner);
        		cc.push('</span><span class="card_label">任务结束时间 : ');
        		cc.push(record.endDate);
        		cc.push('</span><span class="card_label">状态 :');

        		cc.push(convertState(record.state));
        		
        		cc.push('</span></div><div class="card_item_desc"><span class="card_label">创建时间 : ');
        		cc.push(record.createDate);
        		cc.push('</span><span class="card_label">执行者 : ');
        		cc.push(record.assignee);
        		cc.push('</span><span class="card_label">任务相关项 : ');
        		if(record.relateID > 0)
        		{
        			cc.push('<a href="#" onclick="javascript:myTaskJs.openRelate(\'');
        			cc.push(getRelateLink(record.relate,record.relateID));
        			cc.push('\')">');
        			cc.push(record.relateName);
        			cc.push('</a>')
        		}
        		cc.push('</span><span class="card_label">优先级: ');
        		cc.push(convertLevel(record.priority))
        		cc.push('</span><p><span>详细信息 : </span>');
        		cc.push(record.description);
        		cc.push('</p>');
        		
        		cc.push('<div class="task_item_activity">');
        		cc.push('</div>');
        			
        		if(type=='assignee' && (record.state == 'processing' || record.state == 'reject'))
        		{
        			cc.push('<div><a href="javascript:myTaskJs.openApplyApproveTask(');
        			cc.push(record.id);
        			cc.push(',\'');
        			cc.push(idName);
        			cc.push('\')" class="l-btn l-btn-small"><span class="l-btn-left"><span class="l-btn-text" style="width:100px"> 提交审核  </span></span></a></div>')
        		}
        		if(type=='assigner' && record.state == 'approving')
        		{
        			cc.push('<div><a href="javascript:myTaskJs.openApproveTask(');
        			cc.push(record.id);
        			cc.push(',\'');
        			cc.push(idName);
        			cc.push('\')" class="l-btn l-btn-small"><span class="l-btn-left"><span class="l-btn-text" style="width:100px"> 批准  </span></span></a>')
        			
        			cc.push('<a href="javascript:myTaskJs.openRejectTask(');
        			cc.push(record.id);
        			cc.push(',\'');
        			cc.push(idName);
        			cc.push('\')" class="l-btn l-btn-small"><span class="l-btn-left"><span class="l-btn-text" style="width:100px"> 退回  </span></span></a></div>')
        		}
        		cc.push('</div></div>')
        	});
        	
        	$('#Task_records').html(cc.join(''));
        	
    		$('.card_item').bind('mouseover',function(){
    			rowOver(this);
    		});
    		$('.card_item').bind('mouseout',function(){
    			rowOut(this);
    		});
    		$('.card_otherInfo').bind('click',function(){
    			itemClick(this);
    		});
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
	};
	myTaskJs.init = function(){
		
		refresh();
		//loadData("dispatch/utility/task/mine?type="+type+"&start=0&limit="+limit,loadTaskResult);

	};
}( window.myTaskJs = window.myTaskJs || {}, jQuery ));

$(function(){
	myTaskJs.init();
});
</script>
	    <style type="text/css">
    </style>